package com.quickcode.example.singleton;

import com.quickcode.annotation.UnSafe;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by 张波 on 2019/10/23.
 */
@UnSafe
@Slf4j
public class SingletonExample1 {

    private static SingletonExample1 instance = null;

    private SingletonExample1() {
        log.error("调用了构造方法创建了对象");
    }

    public static SingletonExample1 getInstance() {
        if(null == instance) {
            instance = new SingletonExample1();
        }
        return instance;
    }

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newCachedThreadPool();
        for(int i = 0 ; i < 100 ; ++i) {
            executorService.execute(() -> {
                SingletonExample1.getInstance();
            });
        }
        executorService.shutdown();
    }

}
